﻿using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using Newtonsoft.Json;
using PMS.Core.Utils;
using PMS.Web.Framework.Controllers;
using System;
using System.Text.RegularExpressions;

namespace PMS.Web.FrontUI
{
    public static class LayoutExtensions
    {
        /// <summary>
        /// 将.net数据转化成javascript json string供页面使用
        /// </summary>
        /// <param name="html"></param>
        /// <param name="DataModel"></param>
        /// <param name="lowerPropName">是否将各属性值小写替换</param>
        /// <returns></returns>
        public static IHtmlContent Convert2JavascriptJsonString(this IHtmlHelper html, object DataModel, bool lowerPropName = true)
        {
            if (html == null) throw new ArgumentNullException(nameof(html));
            var serializerSettings = new JsonSerializerSettings();

            //API调用时JSON配置序列化时时间格式为时间戳，前端JS需要对格式化时间戳进行Date转化
            serializerSettings.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
            serializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Unspecified;
            string jsonStr = string.Empty;
            if(lowerPropName)
                serializerSettings.ContractResolver = new LowercaseContractResolver();
            jsonStr = JsonConvert.SerializeObject(DataModel, Formatting.Indented, serializerSettings);
            return new HtmlString(jsonStr);
        }

        /// <summary>
        /// 工具函数 用于隐藏大陆地区11位手机号中间四位
        /// </summary>
        /// <param name="html"></param>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public static string HideMobile(this IHtmlHelper html, string mobile)
        {
            if (html == null) throw new ArgumentNullException(nameof(html));
            return CommonHelper.HideMobileInfo(mobile);
        }

        /// <summary>
        /// 工具函数 用于隐藏邮箱信息中'@'前首字母外其他信息
        /// </summary>
        /// <param name="html"></param>
        /// <param name="email"></param>
        /// <returns></returns>
        public static string HideEmail(this IHtmlHelper html, string email)
        {
            if (html == null) throw new ArgumentNullException(nameof(html));
            return CommonHelper.HideEmailInfo(email);
        }
    }
}
